<template>
	<view v-if="goodsInfo.goods_name" class="goods_detail_container">
		<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" circular="true">
			<swiper-item v-for="(item, i) in goodsInfo.pics" @click="preview(i)">
				<image :src="item.pics_big" ></image>
			</swiper-item>
		</swiper>
		
		<view class="goods-detail-box">
			<view class="goods-detail-price">￥{{goodsInfo.goods_price}}</view>
			
			<view class="goods-detail-body">
				<view class="goods-detail-name">{{goodsInfo.goods_name}}</view>
				<view class="goods-detail-sc">
					<uni-icons type="star" size="18" color="grey"></uni-icons>
					<text>收藏</text>
				</view>
			</view>
			
			<view class="kuaidi">快递：免运费</view>
		</view>	
		
		<rich-text :nodes="goodsInfo.goods_introduce"></rich-text>
		
		<view class="goods_nav">
			<uni-goods-nav :fill="true"  :options="options" :buttonGroup="buttonGroup"  @click="onClick" @buttonClick="buttonClick" ></uni-goods-nav>
		</view>
	</view>
</template>

<script>
	import {mapMutations, mapGetters} from 'vuex'
	export default {
		data() {
			return {
				goodsInfo: {},
				 options: [{
							icon: '/static/shop.png',
							text: '店铺',
							infoBackgroundColor:'#007aff',
							infoColor:"red"
						}, {
							icon: '/static/cart2.png',
							text: '购物车',
							info: 0
						}],
				buttonGroup: [{
				  text: '加入购物车',
				  backgroundColor: '#ff0000',
				  color: '#fff'
				},
				{
				  text: '立即购买',
				  backgroundColor: '#ffa200',
				  color: '#fff'
				}
				]
			};
		},
		onLoad(options) {
			const goodsId = options.goods_id 
			this.getGoodsDetail(goodsId)
		},
		computed: {
			...mapGetters('my-cart', ['total'])
		},
		watch: {
			total: {
				handler(newVal) {
					this.options.forEach(item => {
						if (item.text === '购物车') {
							item.info = newVal
						}
					})
				},
				immediate: true
			}
		},
		methods: {
			...mapMutations('my-cart', ['addToCart']),
			async getGoodsDetail(goods_id) {
				const {data:res} = await uni.$http.get('/api/public/v1/goods/detail', {goods_id})
				if (res.meta.status !== 200) return uni.$showMsg()
				
				res.message.goods_introduce = res.message.goods_introduce.replace(/<img/g, '<img style="display:block;"').replace(/webp/g, 'jpg')
				
				this.goodsInfo = res.message
			},
			preview(index) {
				uni.previewImage({
					current: index,
					urls: this.goodsInfo.pics.map(item => item.pics_big)
				})
			},
			
			onClick(options) {
				if(options.content.text == '购物车') {
					uni.switchTab({
						url: '/pages/tabBar/cart/cart'
					})
				}
			},
			
			// 加入购物车
			buttonClick(info) {
				if(info.content.text === '加入购物车') {
					// {goods_id, goods_name, goods_price, goods_count, goods_small_logo, goods_state}
					const goods = {
						goods_id: this.goodsInfo.goods_id,
						goods_name: this.goodsInfo.goods_name,
						goods_price: this.goodsInfo.goods_price,
						goods_count: 1,
						goods_small_logo: this.goodsInfo.goods_small_logo,
						goods_state: true
					}
					
					this.addToCart(goods)
				}
			}
		}
	}
</script>

<style lang="scss">
	swiper {
		height: 750rpx;
		image {
			width: 100%;
			height: 100%;
		}
	}
	
	.goods-detail-box {
		padding: 10px;
		padding-right: 0;
		.goods-detail-price {
			font-size: 18px;
			color: #c00000;
		}
		
		.goods-detail-body {
			display: flex;
			.goods-detail-name {
				font-size: 12px;
				margin-right: 10px;
			}
			
			.goods-detail-sc {
				width: 120px;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				font-size: 12px;
				color: grey;
				border-left: 1px solid #eaeaea;
			}
		}
		
		.kuaidi {
			font-size: 12px;
			color: grey;
		}
		
		
	}
	
	.goods_nav {
		position: fixed;
		width: 100%;
		bottom: 0;
		left: 0;
	}
	
	.goods_detail_container {
		padding-bottom: 50px;
	}
</style>
